package edu.iastate.cs228.hw1;

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Simulates a simplified river ecosystem with bears and fish
 * @author Devon Eilers
 *
 */
public class RiverSimulator 
{
	public static void main(String[] args)  
	{
		Scanner scan = new Scanner(System.in);
		
		System.out.println("River Ecosystem Simulator");
		System.out.println("keys: 1 (random river) 2 (file input) 3 (exit)");
				
		int trialNum = 1;
		int choice = -1;
		
		try
		{
			System.out.print("Trial " + trialNum++ + ": ");
			choice = scan.nextInt();
		}
		catch (InputMismatchException e)
		{
			System.out.println("Invalid input, please try again.");
			System.exit(0);
		}
		
		if (choice < 1 || choice > 3)
		{
			System.out.println("Invalid input, please try again.");
			System.exit(0);
		}

		
		while (choice != 3)
		{
			int nCycles = -1;
			River riverGen = null;
			if (choice==1) // random river
			{

				int rLength;
				
				// checks for input mismatch
				try
				{
					System.out.println("Random River");
					System.out.print("Enter River Length: ");
					rLength = scan.nextInt();
					System.out.print("Enter the number of cycles: ");
					nCycles = scan.nextInt();
				}
				catch (InputMismatchException e)
				{
					System.out.println("Incorrect input please try again");
					break;
				}
				
				System.out.println("");
				
				riverGen = new River(rLength);
			}
			else if (choice==2) // file input
			{
				try
				{
					System.out.print("Enter file name (must be exact): ");
					String fName = scan.next();
					System.out.print("Enter the number of cycles: ");
					nCycles = scan.nextInt();
					System.out.println("");
					
					riverGen = new River(fName);
				}
				catch (IOException e)
				{
					System.out.println("Incorrect file name, try again");
					break;
				}
				catch (InputMismatchException e)
				{
					System.out.println("Incorrect input please try again");
					break;
				}
				

			}
			else if (choice==3)
			{
				System.exit(0);
			}
			else
			{
				System.out.println("Incorrect input, try again");
				break;
			}
			
			for (int i = 1; i <= nCycles; ++i)
			{
				if (i==1)
				{
					System.out.println("Initial River:");
					System.out.println("");
					System.out.println(riverGen.toString());
					System.out.println("");
					
					System.out.println("After cycle " + i + ":");
					System.out.println("");
					riverGen.updateRiver();
					System.out.println(riverGen.toString());
					System.out.println("");
				}
				else
				{
					System.out.println("After cycle " + i + ":");
					System.out.println("");
					riverGen.updateRiver();
					System.out.println(riverGen.toString());
					System.out.println("");
				}
			}
			
			try
			{
				System.out.print("Trial " + trialNum++ + ": ");
				choice = scan.nextInt();
			}
			catch (InputMismatchException e)
			{
				System.out.println("Invalid input, please try again");
				break;
			}
			

		}
		
		scan.close();
	}
}
